using Nop.Core;
using Nop.Core.Infrastructure;
using System;
using System.IO;
using System.Net;
using System.Runtime.Caching;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace Nop.Plugin.Picture.CDN.Licensing
{
	public class Validate
	{
		private const string key = "fAsPaTr7fRUbRese";
		private readonly IWebHelper _webHelper = EngineContext.Current.Resolve<IWebHelper>();
		private readonly CDNPluginSettings _cdnPluginSettings = EngineContext.Current.Resolve<CDNPluginSettings>();
		protected ObjectCache ResponseCache
		{
			get
			{
				return MemoryCache.Default;
			}
		}
		protected string Encrypt(string input, string key)
		{
			byte[] inputArray = Encoding.UTF8.GetBytes(input);
			TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
			tripleDES.Key = Encoding.UTF8.GetBytes(key);
			tripleDES.Mode = CipherMode.ECB;
			tripleDES.Padding = PaddingMode.PKCS7;
			ICryptoTransform cTransform = tripleDES.CreateEncryptor();
			byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
			tripleDES.Clear();
			return Convert.ToBase64String(resultArray, 0, resultArray.Length);
		}
		protected string Decrypt(string input, string key)
		{
			byte[] inputArray = Convert.FromBase64String(input);
			TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
			tripleDES.Key = Encoding.UTF8.GetBytes(key);
			tripleDES.Mode = CipherMode.ECB;
			tripleDES.Padding = PaddingMode.PKCS7;
			ICryptoTransform cTransform = tripleDES.CreateDecryptor();
			byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
			tripleDES.Clear();
			return Encoding.UTF8.GetString(resultArray);
		}
		public void SendDataToApi()
		{
			try
			{
				LicenseValidator licVal = new LicenseValidator();
				licVal.DomainName = this._webHelper.GetStoreLocation();
				licVal.IPAddress = this._webHelper.GetCurrentIpAddress();
                licVal.NopVersion = NopVersion.CurrentVersion;
                licVal.PluginSystemName = "Nop.Plugin.Picture.CDN";
				string LicKey = string.Empty;
				if (!string.IsNullOrEmpty(this._cdnPluginSettings.LicenseKey))
				{
					LicKey = this.Decrypt(this._cdnPluginSettings.LicenseKey, "fAsPaTr7fRUbRese");
				}
				string param = string.Concat(new string[]
				{
					licVal.DomainName,
					"|",
					licVal.IPAddress,
					"|",
					licVal.NopVersion,
					"|",
					licVal.PluginSystemName,
					"|",
					LicKey.ToString()
				});
				param = HttpUtility.UrlEncode(this.Encrypt(param, "fAsPaTr7fRUbRese"));
				string requestURL = "http://shop.xcellence-it.com/LicenseValidator/Validate?param=" + param;
				WebRequest request = WebRequest.Create(requestURL);
				request.Timeout = 50000;
				using (WebResponse response = request.GetResponse())
				{
					using (StreamReader reader = new StreamReader(response.GetResponseStream()))
					{
						string resCode = reader.ReadToEnd();
						resCode = this.Decrypt(resCode, "fAsPaTr7fRUbRese");
						this.ResponseCache.Remove("Response-CDN", null);
						CacheItemPolicy policy = new CacheItemPolicy();
						this.ResponseCache.Add(new CacheItem("Response-CDN", resCode), policy);
					}
				}
			}
			catch (Exception ex)
			{
			}
		}
	}
}
